﻿using System;
using System.Drawing;
using System.Linq;
using ArtMath.PointCollection;
using ArtMath.Helper;
using ArtMath.CommonType;
using ArtMath.Core.Helper;
using ArtMath.Resource;
using ArtMath.Core.Data;
using ArtMath.Attribute;

namespace ArtMath.Component.Circle
{
    /// <summary>
    /// 三点弓形
    /// </summary>
    [Serializable]
    public class ThreePointsArch : ThreePointsPartialCircleBase
    {
        public ThreePointsArch(SketchPad pad, string name, bool addToList = true) : base(pad, name, ToolTypeName.sdgx, addToList)
        {
            pske.Curves = new Curve[] { new Curve() };
        }
        public override PointCollectionsManager Skeleton
        {
            get
            {
                PointCollectionsManager res = base.Skeleton;
                LineSegment ls = new LineSegment(Point2.ToPointD(), Point1.ToPointD());
                res.AddPointCollection(ls);
                res.ConnectHeadAndTail(ls, ske);
                res.ConnectHeadAndTail(ske, ls);
                return res;
            }
        }
        public override PointCollectionsManager PSkeleton
        {
            get
            {
                PointCollectionsManager res = base.PSkeleton;
                LineSegment ls = new LineSegment(Point2.Pxy, Point1.Pxy);
                res.AddPointCollection(ls);
                res.ConnectHeadAndTail(ls, pske);
                res.ConnectHeadAndTail(pske, ls);
                return res;
            }
        }
        public override PointCollectionsManager NewSkeleton
        {
            get
            {
                PointCollectionsManager res = base.NewSkeleton;
                PointCollectionBase tmp = res.PointCollections.Single();
                LineSegment ls = new LineSegment(Point2.NewXY, Point1.NewXY);
                res.AddPointCollection(ls);
                res.ConnectHeadAndTail(ls, tmp);
                res.ConnectHeadAndTail(tmp, ls);
                return res;
            }
        }
        public override PointCollectionsManager NewPSkeleton
        {
            get
            {
                PointCollectionsManager res = base.NewPSkeleton;
                PointCollectionBase tmp = res.PointCollections.Single();
                LineSegment ls = new LineSegment(Point2.NewPxy, Point1.NewPxy);
                res.AddPointCollection(ls);
                res.ConnectHeadAndTail(ls, tmp);
                res.ConnectHeadAndTail(tmp, ls);
                return res;
            }
        }
        [MultilingualDisplayName("ArchCenter")]
        public override PointD Center
        {
            get
            {
                return base.Center;
            }
        }
        [MultilingualDisplayName(null, "ArchCenter", "Displayer")]
        public override PropertyDisplayer CenterDisplayer
        {
            get
            {
                return base.CenterDisplayer;
            }

            set
            {
                base.CenterDisplayer = value;
            }
        }
        //public override double NewCircumference
        //{
        //    get { return NewRadius * (Math.Abs(NewSweep) + Math.Sqrt(2 * (1 - Math.Cos(NewSweep)))); }
        //}
        protected override void ReCalcParams(bool fromBuilding)
        {
            Circumference += ChordLength;
            if (CentralAngle > Math.PI)
            {
                Area += 0.5 * Radius * Radius * Math.Sin(Core.Helper.Angle.RadRound - CentralAngle);
            }
            else
            {
                Area -= 0.5 * Radius * Radius * Math.Sin(CentralAngle);
            }
            if (fromBuilding)
            {
                ShowStatus($"{Resources.Translate("Creating")}{Resources.ColonSeparate}{Name}{Resources.LeftParenthesis}{Resources.Translate("ArcLength")} = {ArcLength}{Resources.CommaSeparate}{Resources.Translate("ChordLength")} = {ChordLength}{Resources.CommaSeparate}{Resources.Translate("ArchCenter")}{Resources.ColonSeparate}{Pad.ShowPoint(Center)}{Resources.CommaSeparate}{Resources.Translate("Radius")} = {Radius}{Resources.CommaSeparate}{Resources.Translate("CentralAngle")} = {CentralAngle}");
            }
        }
        public override void Paint(Graphics gra)
        {
            if (ObjectState == ObjectState.Normal && Visible && IsCreationCompleted)
            {
                Brush bru = FillBrush;
                if (bru != null) pske.Curves[0].Paint(gra, null, bru, Curve.FillType.Path);
                gra.DrawArc(LinePen, PCenter, Pad.Coordinate == null ? 0 : -Pad.Coordinate.RotateAngle.DegreeValue, pske.LongAxisHalfLength, pske.ShortAxisHalfLength, PStart * Core.Helper.Angle.RToD, PSweep * Core.Helper.Angle.RToD);
                gra.DrawLineSegment(LinePen, Point2.Pxy, Point1.Pxy);
            }
            base.Paint(gra);
        }
        public override void PaintTrack(Graphics gra)
        {
            if (ShowTrack_)
            {
                for (int i = 0; i < track.Count; i++)
                {
                    gra.DrawSector(TrackPen, null, track[i].Key, track[i].Value[0], track[i].Value[1], track[i].Value[2], track[i].Value[3], track[i].Value[4]);
                }
            }
            base.PaintTrack(gra);
        }
        public override void PaintVirtualObjects(Graphics gra)
        {
            if (CanDrawVirtual)
            {
                pske.Curves[0].Paint(gra, null, Core.Helper.Painter.VirtualBrush, Curve.FillType.Path);
                gra.DrawArc(PCenter, Pad.Coordinate == null ? 0 : -Pad.Coordinate.RotateAngle.DegreeValue, pske.LongAxisHalfLength, pske.ShortAxisHalfLength, PStart * Core.Helper.Angle.RToD, PSweep * Core.Helper.Angle.RToD);
                if (OrderIndex == 0)
                {
                    gra.DrawLineSegment(SpareCoordinate_, Point1.Pxy);
                }
            }
            if (OrderIndex == 1)
            {
                if (Point1 != null)
                {
                    gra.DrawLineSegment(Point1.Pxy, Point2 == null ? SpareCoordinate_ : Point2.Pxy);
                }
            }
            base.PaintVirtualObjects(gra);
        }
    }
}
